from django.shortcuts import render
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from .models import Area
# Create your views here.
from .serializers import AreaSerializer, SubAreaSerializer
from rest_framework.viewsets import GenericViewSet, ReadOnlyModelViewSet


# ReadOnlyModelViewSet 包含了GenericViewSet, ListModelMixin, RetrieveModelMixin
class AreasViewSet(ReadOnlyModelViewSet, CacheResponseMixin):
    pagination_class = None     # 设置关闭分页

    def get_queryset(self):
        if self.action == 'list':
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()

    def get_serializer_class(self):
        # 根据有没有传pk，判断是差所有还是单个
        if self.action == 'list':
            return AreaSerializer
        else:
            return SubAreaSerializer
